package com.garden.spingboot_garden.service;

import com.garden.spingboot_garden.pojo.User;
import com.garden.spingboot_garden.pojo.dto.UserDto;
import com.garden.spingboot_garden.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service //标记成sping的bean
public class UserServiceImplem implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public User add(UserDto userDto) {
        User userPojo = new User();
        System.out.println("userDto"+userDto);
        BeanUtils.copyProperties(userDto, userPojo);
        System.out.println("userPojo"+userPojo);
        return userRepository.save(userPojo);
    }
    @Override
    public User get(Integer userid) {
        return userRepository.findById(userid).get();
    }

    @Override
    public User update(UserDto userDto) {
        User userPojo = new User();
        BeanUtils.copyProperties(userDto, userPojo);
        return userRepository.save(userPojo);
    }

    @Override
    public void delete(Integer userid) {
//        删除
        userRepository.deleteById(userid);
    }
}
